""" 生成表格 """
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.table import WD_TABLE_ALIGNMENT

# 声明一个word对象
doc = Document()
# 设置字体样式
doc.styles['Normal'].font.name = u'宋体'
doc.styles['Normal'].element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
# ------添加文档标题-------
paragraph = doc.add_paragraph()
run = paragraph.add_run("Test1")
font = run.font
# 设置字体大小
font.size = Pt(24)
# 设置水平居中
paragraph_format = paragraph.paragraph_format
paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

table = doc.add_table(rows=5, cols=6, style="Table Grid")
table.cell(0, 0).merge(table.cell(0, 5))
# table.alignment = WD_TABLE_ALIGNMENT.CENTER
table.cell(0, 0).text = "测试文档"
# 第一行表格水平居中
table.cell(0, 0).paragraphs[0].paragraph_format.alignment = WD_TABLE_ALIGNMENT.CENTER
table.cell(1, 0).text = "姓名"
table.cell(1, 1).text = "张三"
table.cell(1, 2).text = "性别"
table.cell(1, 3).text = "男"
table.cell(1, 4).text = "年龄"
table.cell(1, 5).text = "25"
table.cell(2, 1).merge(table.cell(2, 3))     # 合并单元格，从（2,1）到（2,3）
table.cell(2, 0).text = "公司名称"
table.cell(2, 1).text = "上海艺赛旗软件股份邮箱公司"
table.cell(2, 4).text = "职位"
table.cell(2, 5).text = "售前工程师"
table.cell(3, 1).merge(table.cell(3, 5))
table.cell(3, 0).text = "家庭住址"
table.cell(3, 1).text = "不告诉你"
table.cell(4, 0).text = "手机号"
table.cell(4, 1).text = "13700000000"
table.cell(4, 2).text = "邮箱"
table.cell(4, 3).text = "abc@isearchcom.cn"
table.cell(4, 4).text = "党员"
table.cell(4, 5).text = "■是 □否"

doc.save('./test1.docx')
